import sharedWorkerConstructor from '*?sharedworker';

export const resolveAuthorityType = (value) => {
  switch (value) {
    case 'PAGE':
      return '页面';
    case 'MENU':
      return '菜单';
    case 'BUTTON':
      return '按钮';
    case 'API':
      return '接口';
  }
}; // 解析状态
// 解析状态
export const resolveEnabledName = (value: boolean) => {
  switch (value) {
    case true:
      return '启用';
    case false:
      return '禁用';
  }
};
export const resolveContinuingName = (value: boolean) => {
  switch (value) {
    case true:
      return '持续中';
    case false:
      return '结束';
  }
};
export const resolveGenderName = (value: string) => {
  switch (value) {
    case 'MALE':
      return '男';
    case 'FEMALE':
      return '女';
  }
};
export const resolveParameterDataType = (value) => {
  switch (value) {
    case 'BOOL':
      return 'BOOL';
    case 'BIT':
      return 'BIT';
    case 'BYTE':
      return 'BYTE';
    case 'SHORT':
      return 'SHORT';
    case 'INT':
      return 'INT';
    case 'LONG':
      return 'LONG';
    case 'FLOAT':
      return 'FLOAT';
    case 'DOUBLE':
      return 'DOUBLE';
    case 'STRING':
      return 'STRING';
    case 'DATE':
      return 'DATE';
    case 'TIME':
      return 'TIME';
    case 'DATETIME':
      return 'DATETIME';
    case 'ARRAY':
      return 'ARRAY';
    case 'REAL':
      return 'REAL';
    case 'WORD':
      return 'WORD';
    case 'UNIT32':
      return 'UNIT32';
  }
};
export const resolveParameterValueType = (value) => {
  switch (value) {
    case 'ACCUMULATED':
      return '表显值';
    case 'INSTANT':
      return '实时值';
  }
};
export const resolveParameterAbility = (value) => {
  switch (value) {
    case 'ALARM':
      return '报警';
    case 'STATE':
      return '状态';
    case 'MONITOR':
      return '监控';
    case 'PRODUCE':
      return '产量';
  }
};

export const resolveParameterAbilityColor = (value) => {
  switch (value) {
    case 'ALARM':
      return 'rgba(204,0,0,0.8)';
    case 'STATE':
      return 'rgba(51,51,51,0.8)';
    case 'MONITOR':
      return 'rgba(44,61,173,0.8)';
    case 'PRODUCE':
      return 'rgba(76,196,141,0.8)';
  }
};

export const convertToSelectOptions = (it) => {
  const result = [];
  if (!!!it) {
    return result;
  }
  if (Array.isArray(it)) {
    it.map(iit => convertToSelectOptions(iit)).forEach(iit => result.push(...iit));
  } else {
    result.push({
      key: it.id,
      title: it.name,
      disabled: !it.enabled,
      children: convertToSelectOptions(it.children || [])
    });
  }
  return result;
};

export function convertToTreeOptions(it) {
  let result = [];
  if (!!!it) {
    return result;
  }
  if (Array.isArray(it)) {
    it.map(it => convertToTreeOptions(it)).forEach(it => result.push(...it));
  } else {
    result.push({
      title: it.name,
      key: it.id,
      selectable: true,
      children: convertToTreeOptions(it.children || [])
    });
  }
  return result;
}

export const resolveDeviceRunningState = (value) => {
  switch (value) {
    case 'STOP':
      return '停止';
    case 'RUN':
      return '运行';
    case 'IDLE':
      return '空闲';
    case 'OFFLINE':
      return '离线';
    case 'DEBUG':
      return '调试';
    case 'UNKNOWN':
    default:
      return '未知';
  }
};

export const resolveDeviceRunningStateColor = (value) => {
  switch (value) {
    case 'STOP':
    case 'stop':
      return '#646464';
    case 'RUN':
    case 'run':
      return '#3a57d0';
    case 'IDLE':
    case 'idle':
      return '#8f7777';
    case 'OFFLINE':
    case 'offline':
      return '#b6b6b6';
    case 'DEBUG':
    case 'debug':
      return '#2ea959';
    case 'UNKNOWN':
    case 'unknown':
    default:
      return '#cccccc';
  }
};
export const resolveAlarmLevel = (value) => {
  switch (value) {
    case 'SEVERE':
      return '严重';
    case 'URGENT':
      return '紧急';
    case 'ALARM':
      return '告警';
    case 'NOTICE':
      return '通知';
  }
};
export const resolveAlarmObject = (value) => {
  switch (value) {
    case 'DEVICE':
    default:
      return '设备';
  }
};
export const resolveDeviceActivateState = (value) => {
  switch (value) {
    case 'ACTIVATED':
      return '已激活';
    case 'UNACTIVATED':
    default:
      return '未激活';
  }
};
export const resolveDeviceActivateStateColor = (value) => {
  switch (value) {
    case 'ACTIVATED':
      return '#4098ef';
    case 'UNACTIVATED':
    default:
      return '#646464';
  }
};
export const convertValue = (v: number | string | boolean, type: string) => {
  if (typeof v === 'string') {
    switch (type) {
      case 'INT':
        return Number(parseInt(v as string) as number);
      case 'FLOAT':
        return Number(parseFloat(v as string) as number);
      case 'BOOL':
        return Boolean(!!v as boolean);
      default:
        return v;
    }
  } else {
    return v;
  }
};
